﻿// Le composant de merde de Microsoft ne fonctionne pas
// donc on se tape la page a la main
//
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;

public partial class PasswordRecoveryPage2 : PageBase
{
    protected void Page_Load( object sender, EventArgs e )
    {
        Message.Text = "";

        if ( !Membership.EnablePasswordRetrieval )
        {
            FormsAuthentication.RedirectToLoginPage();
        }

        if ( !IsPostBack )
        {
            Message.Text = Resources.labels.PasswordRecoveryPageLoadMessage; // "Entrez un nom d'utilisateur.";
        }
        else
        {
            VerifyUsername();
        }
    }

    public void VerifyUsername()
    {
        MembershipUser user = Membership.GetUser( UsernameTextBox.Text, false );

        if ( user == null )
        {
            string msg = Resources.labels.PasswordRecoveryUserNotFound;
            msg = msg.Replace( "***user***", Server.HtmlEncode( UsernameTextBox.Text ) );
            Message.Text = msg;
            QuestionLabel.Text = "";
            QuestionLabel.Enabled = false;
            AnswerTextBox.Enabled = false;
            EmailPasswordButton.Enabled = false;
        }
        else
        {
            QuestionLabel.Text = user.PasswordQuestion;
            QuestionLabel.Enabled = true;
            AnswerTextBox.Enabled = true;
            EmailPasswordButton.Enabled = true;
        }
    }


    public void EmailPassword_OnClick( object sender, EventArgs args )
    {
        // Note: Returning a password in clear text using e-mail is not recommended for
        // sites that require a high level of security.
        if ( AnswerTextBox.Text.Trim() == "" )
        {
            Message.Text = Resources.labels.PasswordRecoveryQuestionEmpty; //"Répondez à la question de mot de passe.";
            return;
        }

        try
        {
            string password = Membership.Provider.GetPassword( UsernameTextBox.Text, AnswerTextBox.Text );
            MembershipUser u = Membership.GetUser( UsernameTextBox.Text );
            try
            {
                MailAddress from = new MailAddress( Config.MailFrom );
                MailAddress to = new MailAddress( u.Email );
                MailMessage mail = new MailMessage( from, to );
                mail.Subject = Resources.labels.PasswordRecoveryEmailSubject; // "Votre mot de passe";
                string body = Resources.labels.PasswordRecoveryEmailBody;
                body = body.Replace( "***pass***", Server.HtmlEncode( password ) );
                 //mail.Body = "Votre mot de passe est : " + Server.HtmlEncode( password );
                mail.Body = body;
                mail.Sender = from;

                SmtpClient SmtpMail = new SmtpClient( Config.MailServerSmtp );
                SmtpMail.Send( mail );

                Message.Text = Resources.labels.PasswordRecoverySuccess;
                //Message.Text = "Votre mot de passe vous à été envoyé par email.";
            }
            catch ( SmtpException ex )
            {
                Message.Text = ex.Message;
            }
            catch ( HttpException ex )
            {
                Message.Text = ex.Message;
            }
            catch ( Exception ex )
            {
                Message.Text = ex.Message;
            }
        }
        catch ( MembershipPasswordException e )
        {
            Message.Text = e.Message;
        }
        catch ( System.Configuration.Provider.ProviderException e )
        {
            Message.Text = Resources.labels.PasswordRecoveryFailed;
            //Message.Text = "Erreur lors de la récupération de votre mot de passe : ";
            Message.Text += "<br>" + e.Message;
        }
    }
}
